package com.seecen;

import com.seecen.entity.Student;
import com.seecen.entity.Teacher;
import com.seecen.mapper.StudentMapper;
import com.seecen.mapper.TeacherMapper;
import com.seecen.vo.StudentVO;
import lombok.extern.log4j.Log4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@Log4j
public class Work {
    private static SqlSessionFactory factory = null;

    static {
        InputStream is = Work.class.getClassLoader().getResourceAsStream("mybatis.xml");
        factory = new SqlSessionFactoryBuilder().build(is);
    }
    public static void main(String[] args) {
        // saveTestData();
        // queryLazy();
        SqlSession rollbackSession = null;
        try(SqlSession session = factory.openSession(false)) {
            rollbackSession = session;
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);

            List<StudentVO> list = studentMapper.findAll();
            list.forEach(System.out::println);

            session.commit();
        } catch (Exception e) {
            rollbackSession.rollback();
            log.error(e);
        }
    }

    private static void queryLazy() {
        SqlSession rollbackSession = null;
        List<Student> list = null;
        try(SqlSession session = factory.openSession(false)) {
            rollbackSession = session;
            TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);

            Student student = new Student();
            Teacher teacher = new Teacher();
            teacher.setTeacherId(2);
            student.setTeacher(teacher);
            list = studentMapper.queryList(student);
            // 打印查询到的学生数量
            System.out.println(list.get(0).getName());
            session.commit();
        } catch (Exception e) {
            rollbackSession.rollback();
            log.error(e);
        }
        // mybatis的延迟加载，底层会重新帮你打开新的会话连接查询，然后查询完关闭
        System.out.println(list.get(0).getTeacher().getName());
    }


    private static void saveTestData() {
        SqlSession rollbackSession = null;
        try(SqlSession session = factory.openSession(false)) {
            rollbackSession = session;
            TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);

            Teacher teacher = new Teacher(null, "刘备", "1", null);
            int res = teacherMapper.save(teacher);
            if(res > 0) {
                Student stu1 = new Student(null, "赵飞", "1", teacher);
                Student stu2 = new Student(null, "关羽", "1", teacher);
                List<Student> students = new ArrayList<>(2);
                students.add(stu1);
                students.add(stu2);
                studentMapper.saveList(students);
            }
            session.commit();
        } catch (Exception e) {
            rollbackSession.rollback();
            log.error(e);
        }
    }


}
